package com.ow.dva.module.passingRecord.controller;


import com.ow.dva.module.base.controller.BaseController;
import com.ow.dva.module.base.entity.json.RT;
import com.ow.dva.module.base.entity.param.DvaPage;
import com.ow.dva.module.base.service.ImageService;
import com.ow.dva.module.park.service.RoadService;
import com.ow.dva.module.passingRecord.entity.PassingRecord;
import com.ow.dva.module.passingRecord.service.PassingRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;


/**
 * <p>
 * 过车记录表 前端控制器
 * </p>
 *
 * @author YanZengBao
 * @since 2020-02-19
 * @version V1.0
 */
@Controller
@Api(tags = "过车记录表 相关控制器")
@RequestMapping("/passingRecord")
public class PassingRecordController extends BaseController {

    /**
    * 分页获取过车记录表数据列表信息
    * 创建时间：2020-02-19 YanZengBao
    * 修改时间：2020-02-19 YanZengBao 加注释
    * 修改时间：
    */
    @ApiOperation(value = "分页获取",notes = "批量获取过车记录表信息列表，分页，可携带条件")
    @RequiresPermissions("passingRecord:page")
    @GetMapping(value = "/page")
    public String page(DvaPage<PassingRecord> dvaPage, @RequestParam Map<String, Object> param, Model model){

        DvaPage<PassingRecord> page = passingRecordService.page(dvaPage,param);

        model.addAttribute("page",page);
        model.addAttribute("param",param);
        return "passingRecord/list";
    }

    /**
    * 进入过车记录表添加页面。
    * 创建时间：2020-02-19 YanZengBao
    * 修改时间：2020-02-19 YanZengBao 加注释
    * 修改时间：
    */
    @ApiOperation(value = "进入添加页面",notes = "进入过车记录表添加页面")
    @RequiresPermissions("passingRecord:add")
    @GetMapping(value = "/add")
    public String add(){

        return "passingRecord/add";
    }

    /**
    * 添加过车记录表信息。
    * 创建时间：2020-02-19 YanZengBao
    * 修改时间：2020-02-19 YanZengBao 加注释
    * 修改时间：
    */
    @ApiOperation(value = "添加",notes = "添加单个过车记录表信息")
    @RequiresPermissions("passingRecord:add")
    @PostMapping("/save")
    @ResponseBody
    public RT save(PassingRecord passingRecord){

        return passingRecordService.save(passingRecord) ? RT.ok() : RT.error(RT.ERROR_0101);
    }

    /**
    * 进入过车记录表编辑页面。
    * 创建时间：2020-02-19 YanZengBao
    * 修改时间：2020-02-19 YanZengBao 加注释
    * 修改时间：
    */
    @ApiOperation(value = "进入编辑页面",notes = "进入过车记录表编辑页面")
    @RequiresPermissions("passingRecord:edit")
    @GetMapping(value = "/edit")
    public String edit(String id, Model model){

        model.addAttribute("data",passingRecordService.getById(id));

        return "passingRecord/edit";
    }

    /**
    * 修改过车记录表信息。
    * 创建时间：2020-02-19 YanZengBao
    * 修改时间：2020-02-19 YanZengBao 加注释
    * 修改时间：
    */
    @ApiOperation(value = "修改",notes = "修改单个过车记录表信息")
    @RequiresPermissions("passingRecord:edit")
    @PostMapping("/update")
    @ResponseBody
    public RT update(PassingRecord passingRecord){

        return passingRecordService.updateById(passingRecord) ? RT.ok() : RT.error(1);
    }

    /**
    * 删除单个过车记录表信息。
    * 创建时间：2020-02-19 YanZengBao
    * 修改时间：2020-02-19 YanZengBao 加注释
    * 修改时间：
    */
    @ApiOperation(value = "删除",notes = "删除单个过车记录表信息")
    @RequiresPermissions("passingRecord:delete")
    @PostMapping("/delete")
    @ResponseBody
    public RT delete(@ApiParam(name="要删除的过车记录表ID") String id){

        return passingRecordService.removeById(id) ? RT.ok() : RT.error(1);
    }

    /**
    * 批量删除多个过车记录表信息。
    * 创建时间：2020-02-19 YanZengBao
    * 修改时间：2020-02-19 YanZengBao 加注释
    * 修改时间：
    */
    @ApiOperation(value = "批量删除",notes = "批量删除多个过车记录表信息")
    @RequiresPermissions("passingRecord:delete")
    @PostMapping("/deleteByIds")
    @ResponseBody
    public RT deleteByIds(@ApiParam(name="要删除的过车记录表ID List") @RequestParam List<String> ids){

        return passingRecordService.removeByIds(ids) ? RT.ok() : RT.error(1);
    }

    /**
     * 文件上传
     * @param file 图片
     * @return 上传状态
     */
    @ApiOperation(value = "上传图片",notes = "表单提交图片")
    @PostMapping(value = "/uploadPassPic")
    @ResponseBody
    public RT uploadPassPic(MultipartFile file, String path) throws IOException {
        if(file != null && !file.isEmpty()){
            try {
                String filePath = imageService.uploadPassPic(file,path);
                return RT.ok("上传成功!",filePath);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("图片上传失败：" + e.getMessage());
                return RT.error(RT.ERROR_0101,"图片上传失败!");
            }
        }else{
            return RT.error(RT.ERROR_0102,"没有选择上传文件!");
        }
    }

    /**
     * @author  姜延鹏
     * @Description
     * @param parkId
     * @date  2020/4/22
     * @return  com.ow.dva.module.base.entity.json.RT

     */
    @RequestMapping(value = "/getTodayPassingRecords")
    @ResponseBody
    public RT getTodayPassingRecords(String parkId){

        List<PassingRecord> passingRecords = new ArrayList<>();

        if (parkId.equals("") || parkId == null){
            passingRecords=passingRecordService.getAllTodayPassingRecords();
        }else {
//            Road road = new Road();
//            road.setParkId(parkId);
//            road.setDirectionType(1);
//            Road roads = roadService.selectByPark(road);
//
//            passingRecords = passingRecordService.getTodayPassingRecords(roads.getId());
            passingRecords = passingRecordService.getTodayByPark(parkId);
        }

        Integer count = passingRecords.size();
        return RT.ok(count);
    }

    @RequestMapping(value = "/passInfoId")
    @ResponseBody
    public List<Map> passInfoId(String parkId){

        List<PassingRecord> passingRecords = new ArrayList<>();

        List<Map> mapList = new ArrayList<>();

        Map map = new HashMap();
        map.put("enterNum", 0);//进车位数
        map.put("outNum", 0);//出车位数
        map.put("allNum", 0);//过车位数
        mapList.add(map);
        Integer enterNum = 0;
        Integer outNum = 0;
        Integer allNum = 0;

        if (parkId.equals("") || parkId == null) {
            passingRecords=passingRecordService.getAllTodayPassingRecords();
            allNum = passingRecords.size();
            mapList.get(0).put("allNum", allNum);
            for (PassingRecord p : passingRecords){
                if (p.getDirection() == 0){
                    enterNum = enterNum+1;
                    mapList.get(0).put("enterNum", enterNum);
                }else {
                    outNum = outNum+1;
                    mapList.get(0).put("outNum", outNum);
                }
            }
        }else {
            passingRecords = passingRecordService.getTodayByPark(parkId);
            allNum = passingRecords.size();
            mapList.get(0).put("allNum", allNum);
            for (PassingRecord p : passingRecords){
                if (p.getDirection() == 0){
                    enterNum = enterNum+1;
                    mapList.get(0).put("enterNum", enterNum);
                }else {
                    outNum =outNum+1;
                    mapList.get(0).put("outNum", outNum);
                }
            }
        }
        return mapList;
    }

    @RequestMapping(value = "/vehicleFlow")
    @ResponseBody
    public RT vehicleFlow(String parkId){
        List<PassingRecord> passDataList=new ArrayList<>();
        List<PassingRecord> passDatas=new ArrayList<>();

        if (parkId.equals("") || parkId == null){
            passDataList=passingRecordService.getAllTodayPassingRecords();
        }else {
            passDataList=passingRecordService.getTodayByPark(parkId);
        }

        for (int i = 0; i < 24; i++) {
            PassingRecord passData = new PassingRecord();
            passData.setCount(0);
            for (PassingRecord c : passDataList) {
                int hour = c.getPassTime().getHours();
                if (hour == i) {
                    passData.setCount(passData.getCount() + 1);
                }
            }
            passDatas.add(passData);
        }

        return RT.ok(passDatas);
    }

    @Resource
    PassingRecordService passingRecordService;
    @Resource
    ImageService imageService;
    @Resource
    private RoadService roadService;
}
